home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12290 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  103 lines

  1. Path: news.tiac.net!usenet
  2. From: jnield@versanet.com (jnield@versanet.com)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help: 2 short functions
  5. Date: Sat, 30 Mar 1996 08:57:46 GMT
  6. Organization: VersaNet International
  7. Message-ID: <4jit3o$f7l@news.tiac.net>
  8. References: <4ji734$n6v@masala.cc.uh.edu> <29MAR199622515778@erich.triumf.ca>
  9. Reply-To: jnield@versanet.com
  10. NNTP-Posting-Host: nield.tiac.net
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. bennett@erich.triumf.ca (P.Bennett) wrote:
  14.  
  15. >In article <4ji734$n6v@masala.cc.uh.edu>, st7jr@Rosie.UH.EDU writes...
  16. >>Hi, I have two functions that I'm not too sure about.
  17. >>The first one:
  18. >> 
  19. >>char *get_filename(void)
  20. >>{
  21. >>   char string[20],*stringp;
  22. >>   scanf("%s",string);
  23. >>   stringp=&string[0];
  24. >>   return stringp;
  25. >>}
  26. >>I'm want the function to read in a string and return a pointer to that string.
  27. >>Am I doing it correctly?
  28.  
  29. >You don't need stringp - you could simply do "return string;" EXCEPT that
  30. >string[] will cease to exist when the function returns, and may be over-written
  31. >at any time.
  32.  
  33. >You could do:
  34. >    ....
  35. >    static char string[20];
  36. >    ....
  37. >    return string;
  38.  
  39. >but you must then remember not to call the function again until you have used
  40. >(or copied) the first string.
  41.  
  42. >You have the same problem in the second function.
  43.  
  44. Personally, I would pass the address of an array to the function as a
  45. param, and have the function read the string into the array (remember,
  46. the array name is a pointer to the first element of the array).
  47.  
  48. main()
  49. {
  50.     char string[20];
  51.     get_filename(string);
  52. }
  53.  
  54. void get_filename(char *string)
  55. {
  56.    scanf("%s",string);
  57. }
  58.  
  59. /* see the next example for open_file() */
  60.  
  61. ... seems like a waste doesn't it...
  62.  
  63.  
  64. But if there is a reason you can't do that(??), you might try this:
  65.  
  66. char *get_filename(void)
  67. {
  68.     char *string;
  69.     string = (char *)malloc(20 * sizeof(char) );
  70.         /* allocate memory for 20 chars on the heap */
  71.     scanf("%s", string);
  72.     return(string);
  73. }
  74.  
  75. /* course, you now have to remember to free the memory! */
  76.  
  77. FILE *open_file(char *filenamep)
  78. {
  79.    FILE *fp;
  80.    if ( !(fp=fopen(filenamep,"r")) )
  81.         {failbad();}
  82.                             /* a string literal is turned into a
  83.                                character pointer when it is compiled, so
  84.                                you can use filenamep as the string for fopen.
  85.                                */
  86.    free(filenamep); /* assuming you want to do it now...
  87.                         'string' and all other pointers to the string
  88.                         are now junk. */
  89.    return fp;
  90. }
  91.  
  92.  
  93. Hope this was somewhat helpfull. You might find a lot of usefull info
  94. in the comp.lang.c FAQ at  
  95.  
  96. http://www.eskimo.com/~scs/C-faq.top.html
  97.  
  98. Disclaimer:
  99. I've only been doing this for a year, and I'm all self taught, so you
  100. might not want to trust my advice too much. Check my code with someone
  101. who knows. . .
  102.  
  103.